from django.db import models
import json
from mdeditor.fields import MDTextField
# Create your models here.
class Question(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=255)
    question=MDTextField(verbose_name="题目内容", default=" ", null=True, blank=True)
    #question = models.TextField(default="{'question': '问题内容', 'image_url': ['https://example.com/image1.jpg', 'https://example.com/image2.jpg']}")
    # image_url 假设储存的是图片的URL，而不是文件
    options = models.TextField(default="{'option1': '选项1', 'option2': '选项2', 'option3': '选项3', 'option4': '选项4'}")
    correct_answers = models.TextField(default="{'correct_answer':['option1', 'option2']}")
    #explanation = models.TextField(default="{'content': '解释内容', 'image_url': ['https://example.com/image3.jpg', 'https://example.com/image4.jpg']}")
    explanation= MDTextField(verbose_name="注释讲解", default=" ", null=True, blank=True)
    question_type = models.CharField(max_length=255, choices=[('single', '单选题'), ('multiple', '多选题'), ('true_or_false', '判断题'), ('fill_in_the_blank', '填空题'), ('short_answer', '简答题'), ('matching', '匹配题'), ('essay', '论述题')], default='single')
    class Meta:
        verbose_name = "题目"
        verbose_name_plural = "题目"# 侧边栏

    def __str__(self):
        return str(self.title)
    
    # 添加获取和设置 JSON 数据的方法
    # def get_question(self):
    #     return json.loads(self.question) if self.question else {}
    
    # def set_question(self, data):
    #     self.question = json.dumps(data)

    def get_options(self):
        return json.loads(self.options) if self.options else {}

    def set_options(self, data):
        self.options = json.dumps(data)

    def get_correct_answers(self):
        return json.loads(self.correct_answers) if self.correct_answers else {}

    def set_correct_answers(self, data):
        self.correct_answers = json.dumps(data)

    # def get_explanation(self):
    #     return json.loads(self.explanation) if self.explanation else {}

    # def set_explanation(self, data):
    #     self.explanation = json.dumps(data)

    
    
